package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;
	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean checkAndDeduct(AccountDeductVo accountDeductVo) {
		//修改user_account表
		int count = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(),accountDeductVo.getAmount());
		if (count == 0){
			return false;
		}
		//修改user_account_detail表
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(accountDeductVo.getUserId());
		userAccountDetail.setAmount(accountDeductVo.getAmount());
		userAccountDetail.setOrderNo(accountDeductVo.getOrderNo());
		userAccountDetail.setTradeType(SystemConstant.ACCOUNT_TRADE_TYPE_MINUS);
		userAccountDetail.setTitle(accountDeductVo.getContent());
		int insert = userAccountDetailMapper.insert(userAccountDetail);
		if (insert == 0){
			return false;
		}
		return true;
	}
}
